package com.jsjosem.aftersales.component;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.jsjosem.aftersales.R;

import java.util.Calendar;
import java.util.Locale;

public class SelectDateDialog {
    private Context context;
    private OnClickListener listener;

    private Dialog dialog;
    private TextView tvTitle;
    private TextView txtSelectDate;
    private Button btnCancel;
    private Button btnConfirm;
    private String dateText;

    public SelectDateDialog(Context context, OnClickListener listener) {
        this.context = context;
        this.listener = listener;
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.dialog_select_date, null);

        initView(layout);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(layout);
        builder.setCancelable(false);
        dialog = builder.create();
    }

    private void initView(View layout) {
        tvTitle = layout.findViewById(R.id.txt_title);
        txtSelectDate = layout.findViewById(R.id.txt_select_date);
        btnCancel = layout.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(view -> dialog.dismiss());
        btnConfirm = layout.findViewById(R.id.btn_confirm);
        btnConfirm.setOnClickListener(view -> {
            if (listener != null) {
                if (dateText == null) {
                    Toast.makeText(context, "请选择日期后再确认", Toast.LENGTH_SHORT).show();
                    return;
                }
                listener.onConfirm(dateText);
                close();
            }
        });

        txtSelectDate.setOnClickListener(view -> {
            //获取Calendar对象，用于获取当前时间
            final Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            // 选择日期
            DatePickerDialog datePickerDialog = new DatePickerDialog(context, (datePicker, selectedYear, monthOfYear, dayOfMonth) -> {
                if (selectedYear < year) {
                    Toast.makeText(context, "不能选择过去的时间", Toast.LENGTH_SHORT).show();
                } else {
                    if ((monthOfYear) < month) {
                        Toast.makeText(context, "不能选择过去的时间", Toast.LENGTH_SHORT).show();
                    } else {
                        if (dayOfMonth < day) {
                            Toast.makeText(context, "不能选择过去的时间", Toast.LENGTH_SHORT).show();
                        } else {
                            dateText = String.format(Locale.CHINA, "%d-%d-%d", year, monthOfYear + 1, dayOfMonth);
                            txtSelectDate.setText(dateText);
                        }
                    }
                }
            }, year, month, day);
            //弹出选择日期对话框
            datePickerDialog.show();
        });
    }

    public void close() {
        dialog.dismiss();
    }

    public void setTitle(String text) {
        tvTitle.setText(text);
        tvTitle.setVisibility(View.VISIBLE);
    }

    public void setTitle(int resid) {
        tvTitle.setText(resid);
        tvTitle.setVisibility(View.VISIBLE);
    }

    public void show() {
        dialog.show();
    }

    public interface OnClickListener {
        void onConfirm(String text);
    }
}
